var form,layer,laypage,$,table,upload,laydate,dynamicCondition,dc,top_dc;
layui.config({
    base: "static/layui/module/"
}).extend({
    dynamicCondition: 'dynamicCondition/dynamicCondition'
}).use(['form', 'layer', 'jquery', 'table', 'laydate', 'upload', 'dynamicCondition'], function () {
    form = layui.form,
    layer = layui.layer,
    laypage = layui.laypage,
    $ = layui.jquery,
    table = layui.table,
    upload = layui.upload,
    laydate = layui.laydate
    dynamicCondition = layui.dynamicCondition;

    table.render({
        elem: '#table'
        , title: '列表'
        , url: '/manage/common/list/' + object.url
        , method: 'post'
        , height: 'full-60'
        , page: true //开启分页
        , limit: 20
        , limits: [20, 30, 40, 50, 60, 70, 80, 90, 100]
        , loading: true
        , cols: [object.cols]
        , text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
        , toolbar: '#tableToolbar'
        , defaultToolbar: ['filter']
    });

    var dataFields = object.dataFields;

    top_dc = undefined;

    dc = dynamicCondition.create({
        fields: dataFields
        , tableId: "table"
        , conditionTextId: "#findMsg"
        , type: "complex"
        , requestDataType: "json"
        , counts: 10
        , queryCallBack: function (requestData) {
        }
        , afterOpen: function (_dc) {
            top_dc = _dc;
        }
    });
})
function tableToolbarEnvent(obj) {
    var tableCheckData = table.checkStatus(obj.config.id);
    switch (obj.event) {
        case 'find':
            dc.open();
            break;
        case 'add':
            var index = layer.open({
                type: 2
                , title: '新增'
                , area: ['50%', '100%']
                , content: '/manage/common/addHtml/' + object.url
                , btn: ['保存', '取消']
                , offset: 'r'
                , yes: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                    var data = iframeWindow.getFormDataAndValidate();
                    if (!data[0]) {
                        return;
                    }

                    var master = data[1];
                    var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                    $.post("/manage/common/add", {url: object.url, obj: JSON.stringify(master)}, function (d) {
                        if (d.code == '200') {
                            top.layer.alert("新增成功", {icon: 1});
                            table.reload('table');
                        } else {
                            top.layer.msg("新增失败," + d.msg, {icon: 2});
                        }
                        top.layer.close(shadeIndex);
                        layer.close(index);
                    }, "json")
                }
            });
            break;
        case 'upd':
            var data = tableCheckData.data;
            if (data.length == 0) {
                layer.msg("请选择要修改的数据");
                return;
            }
            ;
            if (data.length != 1) {
                layer.msg("请选择单条数据");
                return;
            }
            ;
            var index = layer.open({
                type: 2
                , title: '修改'
                , area: ['50%', '100%']
                , content: '/manage/common/editHtml/' + object.url + '/' + data[0].id
                , btn: ['保存', '取消']
                , offset: 'r'
                , yes: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                    var d = iframeWindow.getFormDataAndValidate();
                    if (!d[0]) {
                        return;
                    }
                    var master = d[1];
                    master.id = data[0].id;

                    var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                    $.post("/manage/common/update", {url: object.url, obj: JSON.stringify(master)}, function (d) {
                        if (d.code == '200') {
                            top.layer.msg("修改成功", {icon: 1});
                            table.reload('table');
                        } else {
                            top.layer.msg("修改失败," + d.msg, {icon: 2});
                        }
                        top.layer.close(shadeIndex);
                        layer.close(index);
                    }, "json")
                }
            });
            break;
        case 'delete':
            if (tableCheckData.data.length == 0) {
                layer.msg("请选择要删除的数据");
                return;
            }
            top.layer.confirm('是否删除?', {icon: 3, title: '提示'}, function (index) {
                var ids = new Array();
                $.each(tableCheckData.data, function (index, e) {
                    ids.push(e.id);
                });
                var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                $.ajax({
                    url: "/manage/common/delete/" + object.url,
                    type: "post",
                    data: JSON.stringify(ids),
                    contentType: "application/json",
                    success: function (d) {
                        if (d.code == '200') {
                            top.layer.alert("删除成功," + d.msg, {icon: 1});
                            table.reload('table');
                        } else {
                            top.layer.alert("删除失败," + d.msg, {icon: 2});
                        }
                        top.layer.close(shadeIndex);
                    },
                    error: function () {
                        top.layer.close(shadeIndex);
                        alert("删除失败！");
                    }
                });
                top.layer.close(index);
            });
            break;
        case 'disable':
            if (tableCheckData.data.length != 1) {
                layer.msg("请选择要启用|禁用的数据");
                return;
            }
            top.layer.confirm('是否' + tableCheckData.data[0].disable ? '禁用' : '启用' + '?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                var ids = new Array();
                $.each(tableCheckData.data, function (index, e) {
                    ids.push(e.id);
                });
                var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                $.ajax({
                    url: "/manage/common/disable/" + object.url,
                    type: "post",
                    data: JSON.stringify(ids),
                    contentType: "application/json",
                    success: function (d) {
                        if (d.code == '200') {
                            top.layer.msg("操作成功", {icon: 1});
                            table.reload('table');
                        } else {
                            top.layer.msg("操作失败," + d.msg, {icon: 2});
                        }
                        top.layer.close(shadeIndex);
                    },
                    error: function () {
                        top.layer.close(shadeIndex);
                        alert("操作失败！");
                    }
                });
                top.layer.close(index);
            });
            break;
        case 'download':
            layer.confirm('确定要导出数据并下载到本地吗？', function (index) {
                var conditionArray = new Array();
                if (top_dc != undefined) {
                    for (let i in top_dc.cacheCondition) {
                        if (top_dc.cacheCondition[i].conditionValueLeftVal.value != ''
                            || top_dc.cacheCondition[i].conditionValueRightVal.value != '' || top_dc.cacheCondition[i].conditionValueVal.value != '') {
                            var condition = {
                                field: top_dc.cacheCondition[i].conditionFieldVal
                                , type: top_dc.cacheCondition[i].conditionOptionVal
                                , right: top_dc.cacheCondition[i].conditionValueRightVal.value
                                , left: top_dc.cacheCondition[i].conditionValueLeftVal.value
                                , value: top_dc.cacheCondition[i].conditionValueVal.value
                            }
                            conditionArray.push(condition);
                        }
                    }
                }
                window.location.href = "/manage/common/download?url=" + object.url + "&condition=" + encodeURI(JSON.stringify(conditionArray));
                layer.close(index);
            });
            break;
    }
    ;
}